package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.LevelStrUtil;
import com.ugoodtech.mdcc.client.dto.ComplaintSolutionReportAnswer;
import com.ugoodtech.mdcc.client.dto.ComplaintSolutionVO;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.AliSmsProvider;
import com.ugoodtech.mdcc.client.service.ComplaintReportService;
import com.ugoodtech.mdcc.client.service.ComplaintSolutionService;
import com.ugoodtech.mdcc.client.service.ComplaintUserService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.AdminUserVo;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.GetDepartmentNameForSendService;
import com.ugoodtech.mdcc.core.service.UserAdminService;
import com.ugoodtech.mdcc.core.service.WechatService;
import com.ugoodtech.mdcc.core.utils.ComplaintReportUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class}, propagation = Propagation.REQUIRED)
public class ComplaintSolutionServiceImpl implements ComplaintSolutionService {

    private Logger logger = LoggerFactory.getLogger(ComplaintSolutionServiceImpl.class);

    /**
     * 投诉解决方案-Repository
     */
    @Autowired
    private ComplaintSolutionReportRepository complaintSolutionReportRepository;

    /**
     * 投诉解决方案答案-Repository
     */
    @Autowired
    private ComplaintSolutionDataRepository complaintSolutionDataRepository;

    /**
     * 投诉单-Repository
     */
    @Autowired
    private ComplaintReportRepository complaintReportRepository;

    /**
     * 投诉单日志-Repository
     */
    @Autowired
    private ComplaintReportLogRepository complaintReportLogRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private AliSmsProvider aliSmsProvider;

    @Autowired
    private ComplaintReportDataRepository complaintReportDataRepository;

    @Autowired
    private ComplaintSolutionTimeOutRepository complaintSolutionTimeOutRepository;

    @Autowired
    private WechatService wechatService;

    private Integer reportType = null;

    @Autowired
    private ComplaintUserService complaintUserService;

    @Autowired
    private ComplaintReportService complaintReportService;

    @Autowired
    private UserAdminService userAdminService;

    @Autowired
    private GetDepartmentNameForSendService getDepartmentNameForSendService;

    /**
     * 领取投诉报告
     *
     * @return
     */
    @Override
    public JsonResponse claimComplaintReport(User user, Long complaintReportId) {
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (null == userProfile || StringUtils.isEmpty(userProfile.getRealName())) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到当前登录用户！");
        }
        //查询已审核并且未领取的这份报告
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        booleanBuilder.and(qComplaintReport.deleted.eq(false));
        booleanBuilder.and(qComplaintReport.enabled.eq(true));
        booleanBuilder.and(qComplaintReport.id.eq(complaintReportId));
        booleanBuilder.and(qComplaintReport.receive.eq(false));
        booleanBuilder.and(qComplaintReport.audit.eq(true));
        ComplaintReport complaintReport = complaintReportRepository.findOne(booleanBuilder);
        if (null == complaintReport) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉单或该投诉单已被其他人领取");
        }
        complaintReport.setReceive(true);
        complaintReport.setChangeTime(new Date());
        complaintReport.setFollowUser(user);
        complaintReportRepository.save(complaintReport);
        //插入一个记录
        complaintUserService.updateComplaintUser(user, complaintReport);
        //记录操作
        ComplaintReportLog complaintReportLog = new ComplaintReportLog();
        complaintReportLog.setUserName(userProfile.getRealName());
        complaintReportLog.setUser(user);
        complaintReportLog.setReportStatus("领取投诉");
        complaintReportLog.setTime(new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(new Date()));
        complaintReportLog.setComplaintReportId(complaintReportId);
        complaintReportLogRepository.save(complaintReportLog);
        sendGetMessage(complaintReport, userProfile);
        return JsonResponse.successResponse();
    }

    /**
     * 短信通知领取报告
     *
     * @param complaintReport
     * @param userProfile
     */
    private void sendGetMessage(ComplaintReport complaintReport, UserProfile userProfile) {
        //获取管理员手机号
        //获取管理员手机号
        AdminUserVo adminUserVo = userAdminService.getAdminUser();
        StringBuilder telephoneStr = adminUserVo.getTelephoneStr();
        List<User> oppenIdUser = adminUserVo.getUserList();
        oppenIdUser.add(complaintReport.getUser());
        if (null != complaintReport.getLeader() && complaintReport.getLeader() != 0) {
            User user = userRepository.findOne(complaintReport.getLeader());
            if (null != user && StringUtils.isNotEmpty(user.getUsername())) {
                oppenIdUser.add(user);
            }
        } else if (!complaintReport.getNeedUserList().isEmpty()) {
            for (Long userId : complaintReport.getNeedUserList()) {
                User user = userRepository.findOne(userId);
                if (null != user && StringUtils.isNotEmpty(user.getUsername())) {
                    oppenIdUser.add(user);
                }
            }
        }
        ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 194L);
        StringBuffer brandBuffer = new StringBuffer("");
        if (null != complaintReport && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
            for (String brandIdStr : complaintReportData.getAnswerId().split(",")) {
                if (!brandIdStr.equals("447")) {
                    QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(brandIdStr));
                    if (null != questionAnswerOption && StringUtils.isNotEmpty(questionAnswerOption.getName())) {
                        brandBuffer.append(questionAnswerOption.getName() + ",");
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(complaintReportData.getAnswerOther())) {
            brandBuffer.append(complaintReportData.getAnswerOther());
        }
        String customer = complaintReportService.getComplaintReportCustomer(complaintReport.getId());
        try {
            String remark = "已被" + userProfile.getRealName() + "领取，其他人已无法领取跟进，处理详情请在信息查询内查看。";
            String firstStr = "您好，有客户提交了一份投诉。";
            for (User openUser : oppenIdUser) {
                wechatService.pushWechatMessage(openUser, "i0Bxtwki-LD4ytCJ-8VDO8g3ThpSHC5h3ysfSi7oh3s", Constants.WECHAT_PUSH_URL, firstStr,
                        remark, customer, brandBuffer.toString(), complaintReport.getCreationTimeStr(), "", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 提交投诉解决方案
     *
     * @param user
     * @param complaintSolutionReportList
     * @param complaintReportId
     * @return
     */
    @Override
    public JsonResponse submitComplaintSolutionReport(User user, ComplaintSolutionReportAnswer complaintSolutionReportList,
                                                      Long complaintReportId, Long complaintSolutionReportId) {
        ComplaintReport complaintReport = complaintReportRepository.findOne(complaintReportId);
        if (null == complaintReport) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉报告");
        }
        if (complaintReport.getIsFollow().equals(2)) {
            return JsonResponse.errorResponseWithError("抱歉", "该投诉单无法再跟进");
        }
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (null == userProfile || StringUtils.isEmpty(userProfile.getRealName())) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到当前登录用户！");
        }
        //获取本次解决方案的状态
        String solutionAnswerId = "";
        String solutionTime = "";
        for (ComplaintSolutionData data : complaintSolutionReportList.getComplaintSolutionReportList()) {
            if (data.getQuestionId().equals(206L)) {
                solutionAnswerId = data.getAnswerId();
            }
            if (data.getQuestionId().equals(205L)) {
                solutionTime = data.getAnswerOther();
            }
        }
        //已经是自行跟进的报告 更新报告
        if (solutionAnswerId.equals("446")) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
            try {
                Long ET = simpleDateFormat.parse(solutionTime).getTime();
                Long nowTime = System.currentTimeMillis();
                if ((ET - nowTime) > 0) {
                    return JsonResponse.errorResponseWithError("抱歉", "预计完成时间未到达，请选择自行跟进");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return JsonResponse.errorResponseWithError("抱歉", "未知错误");
            }
        }
        Integer isFollow = 2;
        for (ComplaintSolutionData data : complaintSolutionReportList.getComplaintSolutionReportList()) {
            //循环查出报告的跟进状态
            if (data.getQuestionId().equals(206L)) {
                Long answerId = Long.valueOf(data.getAnswerId());
                if (answerId.equals(443L)) {
                    isFollow = 1;
                    break;
                }
            }
        }
        //设置是否自行跟进
        complaintReport.setIsFollow(isFollow);
        complaintReportRepository.save(complaintReport);

        logger.info("创建新的投诉解决方案");
        return saveComplaintSolutionReport(user, userProfile, complaintSolutionReportList, complaintReport);
    }


    /**
     * 创建投诉解决方案
     *
     * @param user
     * @param userProfile
     * @param complaintSolutionReportList
     */
    private JsonResponse saveComplaintSolutionReport(User user, UserProfile userProfile,
                                                     ComplaintSolutionReportAnswer complaintSolutionReportList, ComplaintReport complaintReport) {
        ComplaintSolutionReport complaintSolutionReport = new ComplaintSolutionReport();
        complaintSolutionReport.setUser(user);
        complaintSolutionReport.setComplaintReport(complaintReport);
        complaintSolutionReportRepository.save(complaintSolutionReport);
        String followType = "";
        String needFollowUser = "";
        Date scheduledTime = new Date();
        for (ComplaintSolutionData data : complaintSolutionReportList.getComplaintSolutionReportList()) {
            if (data.getQuestionId().equals(205L)) {
                String scheduledTimeStr = data.getAnswerOther();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
                try {
                    scheduledTime = simpleDateFormat.parse(scheduledTimeStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        for (ComplaintSolutionData data : complaintSolutionReportList.getComplaintSolutionReportList()) {
            if (data.getQuestionId().equals(206L)) {
                Long answerId = Long.valueOf(data.getAnswerId());
                if (answerId.equals(443L)) {
                    //自己跟进
                    reportType = Constants.FOLLOW;
                    followType = "：自行跟进";
                    complaintReport.setIsFollow(1);
                    complaintReport.setScheduledTime(scheduledTime);
                    if (null == complaintReport.getFirstFollowTime()) {
                        complaintReport.setFirstFollowTime(new Date());
                    }
                } else if (answerId.equals(444L)) {
                    //转派跟进
                    reportType = Constants.OTHER_FOLLOW;
                    //区分是转派给上级还是其他销售
                    if (StringUtils.isNotEmpty(data.getAnswerOther())) {
                        //是销售
                        List<Long> userIdList = new ArrayList<>();
                        StringBuffer stringBuffer = new StringBuffer("");
                        for (String userId : data.getAnswerOther().split(",")) {
                            UserProfile needUser = userProfileRepository.findByUserId(Long.valueOf(userId));
                            if (null != needUser && StringUtils.isNotEmpty(needUser.getRealName())) {
                                userIdList.add(Long.valueOf(userId));
                                stringBuffer.append(needUser.getRealName() + ",");
                            }
                        }
                        if (StringUtils.isNotEmpty(stringBuffer)) {
                            needFollowUser = stringBuffer.substring(0, stringBuffer.length() - 1);
                        }
                        complaintReport.setLeader(null);
                        complaintReport.setNeedUserList(userIdList);
                    } else if (StringUtils.isNotEmpty(data.getAnswerOther2())) {
                        //是上级领导
                        complaintReport.setNeedUserList(null);
                        UserProfile needUser = userProfileRepository.findByUserId(Long.valueOf(data.getAnswerOther2()));
                        if (null != needUser && StringUtils.isNotEmpty(needUser.getRealName())) {
                            complaintReport.setLeader(Long.valueOf(data.getAnswerOther2()));
                            needFollowUser = needUser.getRealName();
                        }
                    } else {
                        return JsonResponse.errorResponseWithError("抱歉", "请选择转派人员！");
                    }
                    //设置报告为待领取
                    complaintReport.setIsFollow(0);
                    complaintReport.setReceive(false);
                    complaintReport.setBack(true);
                    complaintReport.setFollowUser(null);
                    complaintReport.setFirstFollowTime(null);
                    complaintReportRepository.save(complaintReport);
                    //todo 短信微信通知
                    followType = "：转交审核";
                    changeComplaintReport_Admin(complaintReport, userProfile);
                } else if (answerId.equals(445L)) {
                    //中断处理 并返回给管理员
                    complaintReport.setIsFollow(0);
                    complaintReport.setReceive(false);
                    complaintReport.setBack(true);
                    complaintReport.setFirstFollowTime(null);
                    complaintReportRepository.save(complaintReport);
                    //todo 微信通知管理员
                    reportType = Constants.STOP;
                    followType = "：中断处理";
                    stopComplaintReport_Admin(complaintReport, userProfile);
                } else if (answerId.equals(446L)) {
                    //已完成
                    reportType = Constants.COMPLETE;
                    followType = "：已完成";
                    complaintReport.setCompleteTime(new Date());
                    complaintReport.setIsFollow(0);
                    complaintReport.setFirstFollowTime(null);
                    completeComplaint(complaintReport);
                }
            }
            data.setComplaintSolutionReport(complaintSolutionReport);
            complaintSolutionDataRepository.save(data);
        }
        //更新投诉报告报告状态
        Date nowDate = new Date();
        complaintReport.setSendScheduledTimeMessage(false);
        complaintReport.setUpdateTime(nowDate);
        complaintReport.setChangeTime(nowDate);
        complaintReport.setReportStatus(reportType);
        //判断是否是自行跟进 如果是就将ID保存 否则设为null
        if (reportType.equals(Constants.FOLLOW)) {
            complaintReport.setComplaintSolutionId(complaintSolutionReport.getId());
        } else {
            complaintReport.setComplaintSolutionId(null);
        }
        //判断报告是否有提交报告时间并且 是否设置提交报告时间
        complaintReport.setSolutionTime(new Date());

        //设置投诉解决方案报告状态
        complaintSolutionReport.setReportType(reportType);
        reportType = null;
        complaintSolutionReportRepository.save(complaintSolutionReport);
        //存储最近一次解决方案ID
        complaintReport.setSolutionId(complaintSolutionReport.getId());
        complaintReportRepository.save(complaintReport);

        //存储每个人的操作记录
        complaintUserService.updateComplaintUser(user, complaintReport);

        //记录操作
        ComplaintReportLog complaintReportLog = new ComplaintReportLog();
        complaintReportLog.setUserName(userProfile.getRealName());
        complaintReportLog.setUser(user);
        complaintReportLog.setReportStatus("创建投诉解决方案" + followType);
        complaintReportLog.setTime(new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(scheduledTime));
        complaintReportLog.setComplaintReportId(complaintReport.getId());
        complaintReportLog.setNeedFollowUser(needFollowUser);
        complaintReportLogRepository.save(complaintReportLog);
        return JsonResponse.successResponse();
    }

    /**
     * 获取投诉方案解决列表
     *
     * @param complaintReprotId
     * @param pageable
     * @return
     */
    @Override
    public JsonResponse getComplaintSolutionList(Long complaintReprotId, Pageable pageable) {
        QComplaintSolutionReport qComplaintSolutionReport = QComplaintSolutionReport.complaintSolutionReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qComplaintSolutionReport.deleted.eq(false));
        builder.and(qComplaintSolutionReport.enabled.eq(true));
        builder.and(qComplaintSolutionReport.complaintReport.id.eq(complaintReprotId));
        Page<ComplaintSolutionReport> complaintSolutionReportPage = complaintSolutionReportRepository.findAll(builder, pageable);
        List<ComplaintSolutionVO> auditList = getResponse(complaintSolutionReportPage.getContent());
        Page<ComplaintSolutionVO> page = new PageImpl(auditList, pageable, complaintSolutionReportPage.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    /**
     * 封装返回类
     *
     * @param complaintSolutionReportList
     * @return
     */
    private List<ComplaintSolutionVO> getResponse(List<ComplaintSolutionReport> complaintSolutionReportList) {
        List<ComplaintSolutionVO> responseList = new ArrayList<>();
        for (ComplaintSolutionReport report : complaintSolutionReportList) {
            ComplaintSolutionVO vo = new ComplaintSolutionVO();
            //设置解决方案报告ID
            vo.setComplaintSolutionReportId(report.getId());
            //设置解决方案创建人
            UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
            if (null == userProfile || StringUtils.isEmpty(userProfile.getRealName())) {
                continue;
            }
            vo.setReportUserName(userProfile.getRealName());
            //设置日期
            ComplaintSolutionData complaintSolutionData = complaintSolutionDataRepository.selectSolutionDate(report.getId(), 200L);
            if (null != complaintSolutionData && StringUtils.isNotEmpty(complaintSolutionData.getAnswerOther())) {
                vo.setCreateTimeStr(complaintSolutionData.getAnswerOther());
            } else {
                vo.setCreateTimeStr(report.getUpdateTimeStr());
            }
            //设置填写的解决状态
            vo.setStatus(ComplaintReportUtil.getReportStatus(report.getReportType()));
            //查询此解决方案是否为超时的解决方案
            List<ComplaintSolutionTimeOut> complaintSolutionTimeOut = complaintSolutionTimeOutRepository.findBySolutionId(report.getId());
            if (!complaintSolutionTimeOut.isEmpty()) {
                vo.setOrange(true);
            }
            responseList.add(vo);
        }
        return responseList;
    }

    /**
     * 投诉单转派发送给管理员
     *
     * @param complaintReport
     * @param userProfile
     */
    private void changeComplaintReport_Admin(ComplaintReport complaintReport, UserProfile userProfile) {
        //获取管理员手机号
        AdminUserVo adminUserVo = userAdminService.getAdminUser();
        List<User> oppenIdUser = adminUserVo.getUserList();
        String customer = complaintReportService.getComplaintReportCustomer(complaintReport.getId());
        String follow = "技术人员";
        if (null != complaintReport.getLeader() && complaintReport.getLeader() != 0) {
            userProfile = userProfileRepository.findByUserId(complaintReport.getLeader());
            if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                follow = userProfile.getRealName();
            }
        } else if (!complaintReport.getNeedUserList().isEmpty()) {
            StringBuffer stringBuffer = new StringBuffer("");
            for (Long userId : complaintReport.getNeedUserList()) {
                userProfile = userProfileRepository.findByUserId(userId);
                if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                    stringBuffer.append(userProfile.getRealName() + ",");
                }
            }
            follow = stringBuffer.substring(0, stringBuffer.length() - 1);
        }
        ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 194L);
        StringBuffer brandBuffer = new StringBuffer("");
        if (null != complaintReport && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
            for (String brandIdStr : complaintReportData.getAnswerId().split(",")) {
                if (!brandIdStr.equals("447")) {
                    QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(brandIdStr));
                    if (null != questionAnswerOption && StringUtils.isNotEmpty(questionAnswerOption.getName())) {
                        brandBuffer.append(questionAnswerOption.getName() + ",");
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(complaintReportData.getAnswerOther())) {
            brandBuffer.append(complaintReportData.getAnswerOther());
        }
        try {
            String createUser = getDepartmentNameForSendService.getUserDepartmentName(complaintReport.getUser());
            String remark = userProfile.getRealName() + "转交给" + follow + "后续跟进，请尽快审核!";
            String firstStr = "投诉转交通知";
            String auditTimeStr = new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(complaintReport.getAuditTime());
            String adminStr = "您好，" + createUser + "在" + complaintReport.getCreationTimeStr() + "提交的" + customer + "的投诉报告状态已变更。";
            String adminKey1 = createUser + "将状态更新为 转交审核";
            for (User openUser : oppenIdUser) {
                wechatService.pushWechatMessage(openUser, "i0Bxtwki-LD4ytCJ-8VDO8g3ThpSHC5h3ysfSi7oh3s", Constants.WECHAT_PUSH_URL, firstStr,
                        remark, customer, brandBuffer.toString(), complaintReport.getCreationTimeStr(), "", "");
                if (!openUser.equals(complaintReport.getUser())) {
                    wechatService.pushWechatMessage(openUser, "K8RDODMnerc2Orj7gbgTYKbalKDRqrJNyX6-s46OUc8", Constants.WECHAT_PUSH_URL, adminStr,
                            "可登录漆星服务进行查看。", adminKey1, auditTimeStr, "", "", "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 投诉单中断通知
     *
     * @param complaintReport
     * @param userProfile
     */
    private void stopComplaintReport_Admin(ComplaintReport complaintReport, UserProfile userProfile) {
        //获取管理员手机号
        //获取管理员手机号
        AdminUserVo adminUserVo = userAdminService.getAdminUser();
        StringBuilder telephoneStr = adminUserVo.getTelephoneStr();
        List<User> oppenIdUser = adminUserVo.getUserList();
        String name = "客户";
        UserProfile nameUser = userProfileRepository.findByUserId(complaintReport.getUser().getId());
        if (null != nameUser && StringUtils.isNotEmpty(nameUser.getRealName())) {
            name = nameUser.getRealName();
        }
        String customer = complaintReportService.getComplaintReportCustomer(complaintReport.getId());
        ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 188L);
        String level = "高级";
        if (null != complaintReportData && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(complaintReportData.getAnswerId()));
            if (null != answerOption && StringUtils.isNotEmpty(answerOption.getName())) {
                level = LevelStrUtil.getLevelStr(answerOption.getName());
            }
        }
        try {
            String createUser = getDepartmentNameForSendService.getUserDepartmentName(complaintReport.getUser());
            aliSmsProvider.stopComplaintReport_Admin(telephoneStr.toString(), name, customer, level, userProfile.getRealName());
            String firstStr = "在" + createUser + "提交的" + customer + "的投诉报告状态已变更";
            String auditTimeStr = new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(complaintReport.getAuditTime());
            String keyWord1 = userProfile.getRealName() + "进行中断处理";
            String adminStr = "您好，" + createUser + "在" + complaintReport.getCreationTimeStr() + "提交的" + customer + "的投诉报告状态已变更。";
            String adminKey1 = createUser + "将状态更新为 中断处理";
            for (User user1 : oppenIdUser) {
                wechatService.pushWechatMessage(user1, "K8RDODMnerc2Orj7gbgTYKbalKDRqrJNyX6-s46OUc8", Constants.WECHAT_PUSH_URL, firstStr,
                        "请登录漆星服务进行查看。", keyWord1, auditTimeStr, "", "", "");
                wechatService.pushWechatMessage(user1, "K8RDODMnerc2Orj7gbgTYKbalKDRqrJNyX6-s46OUc8", Constants.WECHAT_PUSH_URL, adminStr,
                        "可登录漆星服务进行查看。", adminKey1, auditTimeStr, "", "", "");
            }
        } catch (Exception e) {

        }
    }

    /**
     * 完成投诉单 发送短信
     *
     * @param complaintReport
     */
    private void completeComplaint(ComplaintReport complaintReport) {
        //获取管理员手机号
        AdminUserVo adminUserVo = userAdminService.getAdminUser();
        List<User> oppenIdUser = adminUserVo.getUserList();
        oppenIdUser.add(complaintReport.getUser());
        String customer = complaintReportService.getComplaintReportCustomer(complaintReport.getId());
        ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 194L);
        StringBuffer brandBuffer = new StringBuffer("");
        if (null != complaintReport && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
            for (String brandIdStr : complaintReportData.getAnswerId().split(",")) {
                if (!brandIdStr.equals("447")) {
                    QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(brandIdStr));
                    if (null != questionAnswerOption && StringUtils.isNotEmpty(questionAnswerOption.getName())) {
                        brandBuffer.append(questionAnswerOption.getName() + ",");
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(complaintReportData.getAnswerOther())) {
            brandBuffer.append(complaintReportData.getAnswerOther());
        }
        String createUser = getDepartmentNameForSendService.getUserDepartmentName(complaintReport.getUser());
        try {
            String firstStr = "您好，" + createUser + "在" + complaintReport.getCreationTimeStr() + "提交的" + customer + "的投诉报告，现已经跟进完成。";
            for (User user1 : oppenIdUser) {
                wechatService.pushWechatMessage(user1, "4fUT9CPb8xa6whn7O07fMMdvs236LmMMH_HQ1IkHDiE", Constants.WECHAT_PUSH_URL, firstStr,
                        "请登录漆星服务系统查看跟进详情。", brandBuffer.toString(), complaintReport.getCreationTimeStr(), "", "", "");
            }
        } catch (Exception e) {

        }
    }

}
